
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
            <!--<div class="layui-btn-group" style="margin-bottom: 10px;width: 100%;">
                <button class="layui-btn layuiadmin-btn-tags" style="float: right;" data-type="add">添加书单</button> 	
            </div>-->
            
            <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>
            <script type="text/html" id="LAY-good-list-table-img">
	            <img src="{{d.image}}" alt="" style="width:160px;">
	        </script>
	        <script type="text/html" id="LAY-good-list-table-stock">
				{{# if (parseInt(d.position) == 1) { }}  
					<p>492*486px</p>
				{{# } else if(d.position=== 2) { }}  
					<p>489*237px</p>
				{{# } else if(d.position=== 3) { }}  
					<p>489*237px</p>
				{{# } else if(d.position=== 4) { }}  
					<p>1065*330px</p>
				{{# } else { }}  
					<p>1065*300px</p>
				{{# } }} 
	        </script>
	        <script type="text/html" id="LAY-good-list-table-stock11">
				{{# if (parseInt(d.position) == 1) { }}  
					<p>首页左1</p>
				{{# } else if(d.position=== 2) { }}  
					<p>首页右1</p>
				{{# } else if(d.position=== 3) { }}  
					<p>首页右2</p>
				{{# } else if(d.position=== 4) { }}  
					<p>买书订单支付页面</p>
					<p>卖书订单提交成功页面</p>
				{{# } else { }}  
					<p>个人中心活动banner</p>
				{{# } }}  
	        </script>
            <script type="text/html" id="test-table-operate-barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
                {{#  if(d.position=== 5){ }}					  
				    {{#  if(parseInt(d.is_show) == 0){ }}					  
				    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="up">设置前端显示</a>
					{{#  } else { }}
						<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="down">设置前端不显示</a>
					{{#  } }} 
				{{#  } else { }}
					
				{{#  } }}                 
            </script>
            </div>
        </div>
        </div>
    </div>
    </div>
<script>
layui.use(['admin', 'table', 'form', 'upload'], function(){
    var table = layui.table
    ,form = layui.form
    ,admin = layui.admin
    ,upload = layui.upload
    ,view = layui.view
    ,element = layui.element
    ,$ = layui.$
	,cols = [
		[
			{
				align: 'center',
				width: 180,
				templet: '#LAY-good-list-table-img',
				title: '图片',
				field: 'image'
			},{
				align: 'center',
				field: '',
				title: '图片宽高',
				templet: '#LAY-good-list-table-stock'
			}, {
				align: 'center',
				field: 'url',
				title: 'URL'
			}, {
				align: 'center',
				field: 'position',
				title: '位置',
				templet: '#LAY-good-list-table-stock11'
			}, {
				align: 'center',
				fixed: 'right',
				title: '操作',
				toolbar: '#test-table-operate-barDemo'
			}
		]
	];
	table.set({
		headers: { //通过 request 头传递
			'access-token': layui.data('layuiAdmin').access_token
		}
	});
	table.render({
		elem: '#test-table-operate',
		url: 'https://bookshop.dingdangtoy.com/api/admin/banners',
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
				"data": res.data //解析数据列表			
			}
		},
		cols: cols,
		page: false
	});
    
    //监听工具条
    table.on('tool(test-table-operate)', function(obj){
      var data = obj.data;
      if(obj.event === 'edit'){
        admin.popup({
        title: '编辑banner'
        ,area: ['80%', '80%']
        ,id: 'LAY-popup-content-tags'
        ,success: function(layero, index){
          view(this.id).render('banner/all/add', data).done(function(){
            form.render(null, 'layuiadmin-form-tags');         
            if (parseInt(data.position) == 1) {
            	$('#width_1').text('492*486px');
            	$('#add-goods-upload-img').css('width','164px');
            	$('#add-goods-upload-img').css('height','162px')
            } else if (parseInt(data.position) == 2) {
            	$('#width_1').text('489*237px');
            	$('#add-goods-upload-img').css('width','163px');
            	$('#add-goods-upload-img').css('height','78px')
            } else if (parseInt(data.position) == 3) {
            	$('#width_1').text('489*237px');
            	$('#add-goods-upload-img').css('width','163px');
            	$('#add-goods-upload-img').css('height','78px')
            } else if (parseInt(data.position) == 4) {
            	$('#width_1').text('1065*330px');
            	$('#add-goods-upload-img').css('width','255px');
            	$('#add-goods-upload-img').css('height','120px')
            } else {
            	$('#width_1').text('1065*300px');
            	$('#add-goods-upload-img').css('width','355px');
            	$('#add-goods-upload-img').css('height','100px')
            }
            //上传图片
			  upload.render({
				elem: '#add-goods-upload-btn',
				url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
				accept: 'images',
				method: 'get',
				acceptMime: 'image/*',
				done: function(res) {
					$('#add-goods-upload-img').attr('src', res.data.url);
					$('#add-goods-upload-image').val(res.data.url)
					//$(this.item).prev("div").children("input").val(res.data.url)
				}
			  }); 
            //监听提交
            form.on('submit(layuiadmin-app-tags-submit)', function(data){	
              var title = data.field.title; //获取提交的字段
              var sort = data.field.sort;
              //提交 Ajax 成功后，关闭当前弹层并重载表格
			        layer.confirm('是否执行此操作', function(index){
			   	        $.ajax({
						    type: "PUT",
						    dataType: "json",
						    url: "https://bookshop.dingdangtoy.com/api/admin/banner/update",
						    data: {
						    	id:data.field.id,
						    	url:data.field.url,
						    	image:data.field.image,
						    	position:data.field.position
						    },
						    headers: { //通过 request 头传递
				              'access-token': layui.data('layuiAdmin').access_token
				            },
									success: function(data){
									    if (parseInt(data.code) == 0) {
									    	layer.msg('操作成功');
										    layui.table.reload('test-table-operate');
									    } else {
									    	layer.msg(data.message);
									    }
									},
									error: function(msg){
									  console.log(msg)
									}
							});
			        });			         
				layer.close(index); //执行关闭 
            });
          });
        }
      });
      } else if(obj.event === 'up'){
        layer.confirm('设置前端显示?', function(index){
      		$.ajax({
				type: "PUT",
				dataType: "json",
				url: "https://bookshop.dingdangtoy.com/api/admin/banner/update_show",
				data: {
					id: data.id,
					is_show: 1
				},
				headers: { //通过 request 头传递
					'access-token': layui.data('layuiAdmin').access_token
				},
				success: function(data) {
					layer.msg('操作成功');
					layui.table.reload('test-table-operate');
					layer.close(index); //执行关闭 
				},
				error: function(msg) {
					console.log(msg)
				}
			});
        });
      } else if(obj.event === 'down'){
        layer.confirm('设置前端不显示?', function(index){
      		$.ajax({
				type: "PUT",
				dataType: "json",
				url: "https://bookshop.dingdangtoy.com/api/admin/banner/update_show",
				data: {
					id: data.id,
					is_show: 0
				},
				headers: { //通过 request 头传递
					'access-token': layui.data('layuiAdmin').access_token
				},
				success: function(data) {
					layer.msg('操作成功');
					layui.table.reload('test-table-operate');
					layer.close(index); //执行关闭 
				},
				error: function(msg) {
					console.log(msg)
				}
			});
        });
      }
    });
//var active = {
//  add: function(){
//    admin.popup({
//      title: '添加名家'
//      ,area: ['80%', '80%']
//      ,id: 'LAY-popup-content-tags'
//      ,success: function(layero, index){
//        view(this.id).render('book-list/list/add').done(function(){
//          form.render(null, 'layuiadmin-form-tags');
//            //上传图片
//			  upload.render({
//				elem: '#add-goods-upload-btn',
//				url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
//				accept: 'images',
//				method: 'get',
//				acceptMime: 'image/*',
//				done: function(res) {
//					console.log(res.data.url)
//					$('#add-goods-upload-img').attr('src', res.data.url);
//					$('#add-goods-upload-image').val(res.data.url)
//					//$(this.item).prev("div").children("input").val(res.data.url)
//				}
//			  }); 
//			  upload.render({
//				elem: '#add-goods-upload-btn1',
//				url: 'https://bookshop.dingdangtoy.com/api/admin/image/store',
//				accept: 'images',
//				method: 'get',
//				acceptMime: 'image/*',
//				done: function(res) {
//					console.log(res.data.url)
//					$('#add-goods-upload-img1').attr('src', res.data.url);
//					$('#add-goods-upload-image1').val(res.data.url)
//					//$(this.item).prev("div").children("input").val(res.data.url)
//				}
//			  });
//          //监听提交
//          form.on('submit(layuiadmin-app-tags-submit)', function(data){	
//            var title = data.field.title; //获取提交的字段
//            var sort = data.field.sort;
//            //提交 Ajax 成功后，关闭当前弹层并重载表格
//			        layer.confirm('是否执行此操作', function(index){
//			   	        $.ajax({
//						    type: "POST",
//						    dataType: "json",
//						    url: "https://bookshop.dingdangtoy.com/api/admin/author/store",
//						    data: {
//						    	author_name:data.field.author_name,
//						    	country:data.field.country,
//						    	author_image:data.field.author_image,
//						    	intro:data.field.intro,
//						    	list_image:data.field.list_image
//						    },
//						    headers: { //通过 request 头传递
//				              'access-token': layui.data('layuiAdmin').access_token
//				            },
//									success: function(data){
//									    if (parseInt(data.code) == 0) {
//									    	layer.msg('操作成功');
//										    layui.table.reload('test-table-operate');
//									    } else {
//									    	layer.msg(data.message);
//									    }
//									},
//									error: function(msg){
//									  console.log(msg)
//									}
//							});
//			        });			         
//				layer.close(index); //执行关闭 
//          });
//        });
//      }
//    });
//  }
//  }
//  
//$('.layui-btn.layuiadmin-btn-tags').on('click', function(){
//  var type = $(this).data('type');
//  active[type] ? active[type].call(this) : '';
//}); 
  
  });
</script>